package com.xiaowen.projectData.service.impl;

import com.google.common.collect.Lists;
import com.xiaowen.codeData.service.CodeDataService;
import com.xiaowen.projectData.service.ProjectDataExportService;
import com.xiaowen.projectData.service.ProjectDataService;
import com.xiaowen.projectDataTitle.dto.ProjectDataTitleDTO;
import com.xiaowen.projectDataTitle.service.ProjectDataTitleService;
import com.xiaowen.tool.ObjUtils;
import com.xiaowen.tool.excle.ExcelExportDTO;
import com.xiaowen.tool.excle.ExcelTitle;
import com.xiaowen.tool.excle.PoiExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProjectDataExportServiceImpl implements ProjectDataExportService {

    @Autowired
    private CodeDataService codeDataService;

    @Autowired
    private ProjectDataTitleService projectDataTitleService;

    @Autowired
    private ProjectDataService projectDataService;

    @Override
    public void exportFile(Integer projectId, HttpServletResponse response) {
        try {
            OutputStream out = response.getOutputStream();
            List<ExcelExportDTO> exportDTOList = Lists.newArrayList();
            //todo 准备数据
            ExcelExportDTO dataExcelExportDTO = getDataExcelExportDTO(projectId);
            if (ObjUtils.isNotEmpty(dataExcelExportDTO)){
                exportDTOList.add(dataExcelExportDTO);
            }
            ExcelExportDTO codeExcelExportDTO = codeDataService.getCodeExcelExportDTO(projectId);
            if (ObjUtils.isNotEmpty(codeExcelExportDTO)){
                exportDTOList.add(codeExcelExportDTO);
            }
            Workbook workbook = PoiExcelUtils.createExcel(exportDTOList);
            if (ObjUtils.isEmpty(workbook)){
                log.error("导出生成数据错误");
            }
            workbook.write(out);
            out.flush();
        } catch (Exception e){
            log.error("导出项目数据错误: {}",e);
            e.printStackTrace();
        }
    }

    private ExcelExportDTO getDataExcelExportDTO(Integer projectId) {
        ProjectDataTitleDTO projectDataTitleDTO = projectDataTitleService.queryProjectDataTitleByProjectId(projectId);
        if (ObjUtils.isEmpty(projectDataTitleDTO) || CollectionUtils.isEmpty(projectDataTitleDTO.getProjectDataTitleList())){
            return null;
        }
        List<ExcelTitle> titleList = projectDataTitleDTO.getProjectDataTitleList().stream().map(v->{
            ExcelTitle excelTitle = new ExcelTitle(ObjUtils.toString(v.get("title")));
            return excelTitle;
        }).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(titleList)){
            return null;
        }
        titleList.add(new ExcelTitle("关联码表名称"));
        titleList.add(new ExcelTitle("关联码表编码"));
        List<Map<String, Object>> dataList = projectDataService.queryProjectDataListWithCodeByProjectId(projectId);
        ExcelExportDTO excelExportDTO = new ExcelExportDTO();
        excelExportDTO.setTitleAry(titleList);
        excelExportDTO.setDataList(dataList);
        return excelExportDTO;
    }
}
